import request from '@/utils/request'
import cache from '@/plugins/cache'

// 查询菜单列表
export function listMenu(query) {
  return request({
    url: '/system/menu/list',
    method: 'get',
    params: query
  })
}

// 查询菜单详细
export function getMenu(menuId) {
  return request({
    url: '/system/menu/' + menuId,
    method: 'get'
  })
}

// 查询菜单下拉树结构
export function treeselect() {
  return request({
    url: '/system/menu/treeselect',
    method: 'get'
  })
}

// 根据角色ID查询菜单下拉树结构
export function roleMenuTreeselect(roleId) {
  return request({
    url: '/system/menu/roleMenuTreeselect/' + roleId,
    method: 'get'
  })
}

// 新增菜单
export function addMenu(data) {
  return request({
    url: '/system/menu',
    method: 'post',
    data: data
  })
}

// 修改菜单
export function updateMenu(data) {
  return request({
    url: '/system/menu',
    method: 'put',
    data: data
  })
}

// 删除菜单
export function delMenu(menuId) {
  return request({
    url: '/system/menu/' + menuId,
    method: 'delete'
  })
}

/** 处理设置了”路由参数“时候的分库问题*/
export function handlerUpdateBbName(form){
  let tempPath = form.component;
  console.log(tempPath)
  let query = form.query;
  if(tempPath !== null && tempPath !== undefined && tempPath !== '' || tempPath != null && tempPath.trim() !== ''){
    let firstChar = tempPath.substring(0, 1);
    if(firstChar === "/"){
    }else{
      tempPath = "/" + tempPath;
    }
    let regex = new RegExp("/", 'g');
    let number = tempPath.match(regex).length;
    if(number >= 3){
      let index = -1;
      let tempIndex = tempPath.indexOf("/");
      let num = 0;
      while (tempIndex !== -1){
        num ++;
        if(num === 3){
          index = tempIndex;
        }
        tempIndex = tempPath.indexOf("/",tempIndex + 1);
      }
      tempPath = tempPath.substring(0,index);
    }
  }

  if(query !== undefined && query !== '' || query != null && query.trim() !== ''){
    let tempQuery = JSON.parse(query);
    let tempIsSetDbName = tempQuery["dbName"];
    if(tempPath !== undefined && tempPath !== '' || tempPath != null && tempPath.trim() !== ''){
      if(tempIsSetDbName){
        cache.local.set(tempPath,tempIsSetDbName);
      }else{
        cache.local.remove(tempPath);
      }
    }
  }else{
    if(tempPath !== undefined && tempPath !== '' || tempPath != null && tempPath.trim() !== ''){
      cache.local.remove(tempPath);
    }
  }
}
